package com.example.openguass.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.openguass.entity.Customer;
import com.example.openguass.mapper.CustomerMapper;
import com.example.openguass.service.CustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.openguass.vo.CustomerPageVO;
import com.example.openguass.vo.CustomerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xa
 * @since 2022-11-17
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    private CustomerMapper customerMapper;
    @Autowired
    private void setCustomerMapper(CustomerMapper customerMapper){
        this.customerMapper = customerMapper;
    }

    @Override
    public Boolean exit(String loginname) {
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("loginname",loginname);
        return customerMapper.selectCount(wrapper) > 0;
    }

    @Override
    public Boolean insert(CustomerVO customerVO) {
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerVO,customer);
        customer.setCreateDate(LocalDateTime.now());
        return customerMapper.insert(customer) > 0;
    }

    @Override
    public Boolean update(CustomerVO customerVO) {
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerVO,customer);
        customer.setUpdateDate(LocalDateTime.now());
        return customerMapper.updateById(customer) > 0;
    }

    @Override
    public IPage<Customer> findPage(CustomerPageVO pageVO) {
        Page<Customer> page = new Page<>(pageVO.getPageNo(),pageVO.getPageSize());
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        return customerMapper.selectPage(page,wrapper);
    }
}
